package com.example.leetdemo.process;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class Solution2476 {

    public List<List<Integer>> closestNodes(TreeNode root, List<Integer> queries) {
        List<List<Integer>> list = new ArrayList<>();
        for (int  k : queries) {
            int left = -1, right = -1;
            TreeNode temp = root;
            while (temp != null) {
                if (temp.val == k) {
                    left = k;
                    right = k;
                    break;
                } else if (temp.val > k) {
                    if (right == temp.val) {
                        break;
                    }
                    right = temp.val;
                    temp = temp.left;
                } else {
                    if (temp.val == left) {
                        break;
                    }
                    left = temp.val;
                    temp = temp.right;
                }
            }
            List<Integer> list1 = new ArrayList<>();
            list1.add(left);
            list1.add(right);
            list.add(list1);
        }
        return list;
    }

    public void binary(List<Integer> arr, int target, List<Integer> result) {

        if (arr.get(0) > target) {
            result.add(-1);
            result.add(arr.get(0));
            return;
        }
        if (arr.get(arr.size() - 1) < target) {
            result.add(arr.get(arr.size()) - 1);
            result.add(-1);
            return;
        }
        int left = 0, right = arr.size() - 1;
        while (left < right) {
            int i = (right - left) / 2 + left;
            if (arr.get(i) == target) {
                result.add(target, target);
                return;
            } else if (arr.get(i) > target) {
                right = i;
            } else {
                left = i;
            }
        }
        result.add(arr.get(left));
        result.add(arr.get(right));
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public void solution() {
        SortedMap<Integer, String> map = new TreeMap<>();
        map.put(0, "0");
        map.put(1, "1");
        map.put(2, "2");
        SortedMap<Integer, String> head = map.headMap(1);
        System.err.println(head.lastKey());

    }
}
